package com.pojo;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class PostProcessTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("postprocess.xml");
        MyTestBean myTestBean = (MyTestBean) applicationContext.getBean("myTestBean");
        System.out.println(myTestBean.toString());


        // 搜索类加载路径下的beans.xml文件创建Resource对象
        Resource isr = new ClassPathResource("postprocess.xml");
        // 创建默认的BeanFactory容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 让默认的BeanFactory容器加载isr对应的XML配置文件
        new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(isr);
        // 获取容器中的Bean后处理器
        BeanPostProcessor bp = (BeanPostProcessor)beanFactory.getBean("myBeanPostProcessor");
        // 注册Bean后处理器
        beanFactory.addBeanPostProcessor(bp);
        MyTestBean p = (MyTestBean)beanFactory.getBean("myTestBean");
        System.out.println(p.getName());

//
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("postprocess.xml");
//        MyTestBean myTestBean = (MyTestBean) applicationContext.getBean("myTestBean");
//        System.out.println(myTestBean.toString());
    }

}
